//计算角色战斗属性
package role

import (
	"sanguo/node/common/GlobalConstData"
	"sanguo/node/common/attr"
	"sanguo/node/common/battleAttr"
	com_battleAttr "sanguo/node/common/battleAttr"
	"sanguo/node/common/com_type"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role/bag"
	"sanguo/node/node_game/module/role/battle_attr"
	table_GamePlay "sanguo/node/table/excel/table/GamePlay"
	table_RoleLev "sanguo/node/table/excel/table/RoleLevel"
	table_Soul "sanguo/node/table/excel/table/Soul"
	table_SoulEnchant "sanguo/node/table/excel/table/SoulEnchant"
	table_StarMap "sanguo/node/table/excel/table/StarMap"
	table_Weapon "sanguo/node/table/excel/table/Weapon"
	table_WeaponStar "sanguo/node/table/excel/table/WeaponStar"
	cs_message "sanguo/protocol/cs/message"
)

//战斗属性
func (this *Role) GetRoleBattleAttr() *battle_attr.RoleBattleAttr {
	return this.battleAttr
}

func (this *Role) InitRoleBattleAttr(attrs []*battle_attr.BattleAttr) {
	this.battleAttr = &battle_attr.RoleBattleAttr{}
	this.battleAttr.Init(attrs)
}

func (this *Role) NewInitBattleAttr() {
	node_game.Infof("RoleId:%d new roleModule BattleAttr  \n", this.GetID())

	battleAttrs := make([]*battle_attr.BattleAttr, battleAttr.AttrMax)
	for i := 0; i < com_battleAttr.AttrMax; i++ {
		battleAttr := &battle_attr.BattleAttr{
			Idx: int32(i),
			Val: 0,
		}
		battleAttrs[battleAttr.Idx] = battleAttr
	}
	this.InitRoleBattleAttr(battleAttrs)
}

//计算战斗属性
func (this *Role) TotalBattleAttr() {

	//加载等级基础属性
	this.LoadBattleAttrByLevel()
	//加载主武器属性
	//this.AddBattleAttrByWeapon()
	//武魂属性
	this.AddBattleAttrBySouls()
	//兵器谱
	this.AddWeaponAddition()
	//星图
	this.AddStarMap()

	//最后计算百分比加成
	//this.AddAddition()

	//发送更新后的属性
	this.OnBattleAttrDirty()
}

//通过等级加载基础属性
func (this *Role) LoadBattleAttrByLevel() {
	//当前角色等级
	level, _ := this.GetRoleAttr().GetAttr(attr.Level)
	level_attr := table_RoleLev.Table_.IDMap[int32(level)]
	if level_attr == nil {
		return
	}

	roleBattleAttr := this.GetRoleBattleAttr()

	roleBattleAttr.SetBattleAttr(battleAttr.Attack, level_attr.Attack)
	roleBattleAttr.SetBattleAttr(battleAttr.AttackPower, level_attr.AttackPower)
	roleBattleAttr.SetBattleAttr(battleAttr.Defenses, level_attr.Defenses)
	roleBattleAttr.SetBattleAttr(battleAttr.DefensesPower, level_attr.DefensesPower)
	roleBattleAttr.SetBattleAttr(battleAttr.HitPoint, level_attr.HitPoint)
	roleBattleAttr.SetBattleAttr(battleAttr.HPPower, level_attr.HPPower)
	roleBattleAttr.SetBattleAttr(battleAttr.ElementalDefenses, level_attr.ElementalDefenses)
	roleBattleAttr.SetBattleAttr(battleAttr.ElementalDefensesPower, level_attr.ElementalDefensesPower)
	roleBattleAttr.SetBattleAttr(battleAttr.ArmorPoiseHealth, level_attr.ArmorPoiseHealth)
	roleBattleAttr.SetBattleAttr(battleAttr.PoiseDamage, level_attr.PoiseDamage)
	roleBattleAttr.SetBattleAttr(battleAttr.PoiseDamagePower, level_attr.PoiseDamagePower)
	roleBattleAttr.SetBattleAttr(battleAttr.MovementSpeed, level_attr.MovementSpeed)
	roleBattleAttr.SetBattleAttr(battleAttr.MovementSpeedPower, level_attr.MovementSpeedPower)
	roleBattleAttr.SetBattleAttr(battleAttr.AwakenEnergy, level_attr.AwakenEnergy)
	roleBattleAttr.SetBattleAttr(battleAttr.AwakenEnergyGain, level_attr.AwakenEnergyGain)
	roleBattleAttr.SetBattleAttr(battleAttr.AwakenEnergyGainSpeed, level_attr.AwakenEnergyGainSpeed)
	roleBattleAttr.SetBattleAttr(battleAttr.AwakenEnergyCost, level_attr.AwakenEnergyCost)
	roleBattleAttr.SetBattleAttr(battleAttr.AwakenEnergyLessCost, level_attr.AwakenEnergyLessCost)
	roleBattleAttr.SetBattleAttr(battleAttr.MusouEnergy, level_attr.MusouEnergy)
	roleBattleAttr.SetBattleAttr(battleAttr.MusouEnergyGain, level_attr.MusouEnergyGain)
	roleBattleAttr.SetBattleAttr(battleAttr.MusouEnergyGainSpeed, level_attr.MusouEnergyGainSpeed)
	roleBattleAttr.SetBattleAttr(battleAttr.CritChance, level_attr.CritChance)
	roleBattleAttr.SetBattleAttr(battleAttr.CritPower, level_attr.CritPower)
	roleBattleAttr.SetBattleAttr(battleAttr.CritDamage, level_attr.CritDamage)
	roleBattleAttr.SetBattleAttr(battleAttr.DamagePower, level_attr.DamagePower)
	roleBattleAttr.SetBattleAttr(battleAttr.DefensesReduction, level_attr.DefensesReduction)
	roleBattleAttr.SetBattleAttr(battleAttr.FireAttack, level_attr.FireAttack)
	roleBattleAttr.SetBattleAttr(battleAttr.FireAttackPower, level_attr.FireAttackPower)
	roleBattleAttr.SetBattleAttr(battleAttr.IceAttack, level_attr.IceAttack)
	roleBattleAttr.SetBattleAttr(battleAttr.IceAttackPower, level_attr.IceAttackPower)
	roleBattleAttr.SetBattleAttr(battleAttr.LightningAttack, level_attr.LightningAttack)
	roleBattleAttr.SetBattleAttr(battleAttr.LightningAttackPower, level_attr.LightningAttackPower)
	roleBattleAttr.SetBattleAttr(battleAttr.AttackPowerToAir, level_attr.AttackPowerToAir)
	roleBattleAttr.SetBattleAttr(battleAttr.AttackPowerToFall, level_attr.AttackPowerToFall)
	roleBattleAttr.SetBattleAttr(battleAttr.AttackPowerToHumanGrass, level_attr.AttackPowerToHumanGrass)
	roleBattleAttr.SetBattleAttr(battleAttr.AttackPowerToInfernal, level_attr.AttackPowerToInfernal)
	roleBattleAttr.SetBattleAttr(battleAttr.AttackPowerToBoss, level_attr.AttackPowerToBoss)
	roleBattleAttr.SetBattleAttr(battleAttr.PhysicalResistance, level_attr.PhysicalResistance)
	roleBattleAttr.SetBattleAttr(battleAttr.ElementalResistance, level_attr.ElementalResistance)
	roleBattleAttr.SetBattleAttr(battleAttr.MusouAttack, level_attr.MusouAttack)
	roleBattleAttr.SetBattleAttr(battleAttr.MusouAttackPower, level_attr.MusouAttackPower)
	roleBattleAttr.SetBattleAttr(battleAttr.AwakenAttack, level_attr.AwakenAttack)
	roleBattleAttr.SetBattleAttr(battleAttr.AwakenAttackPower, level_attr.AwakenAttackPower)
	roleBattleAttr.SetBattleAttr(battleAttr.ExpPower, 0)
	roleBattleAttr.SetBattleAttr(battleAttr.GoldPower, 0)
	roleBattleAttr.SetBattleAttr(battleAttr.CritAllResistance, 0)
	roleBattleAttr.SetBattleAttr(battleAttr.SkillEnergy, 0)
	roleBattleAttr.SetBattleAttr(battleAttr.SkillEnergyGainSpeed, 0)
}

//加载主武器的属性
func (this *Role) AddBattleAttrByWeapon() {
	weaponId := this.GetWeapon1()
	weaponDef := table_Weapon.Table_.IDMap[weaponId]
	if weaponDef == nil {
		return
	}
}

//加武魂的属性 主武器上的随机+基础+套装 副武器的基础+随机
func (this *Role) AddBattleAttrBySouls() {
	currentLevel, _ := this.GetRoleAttr().GetAttr(attr.Level)
	//武魂功能开启验证
	if int32(currentLevel) < table_GamePlay.Table_.IDMap[com_type.GamePaly_Soul].UnlockLevel {
		return
	}

	weapon1 := this.GetRoleWeapon().GetWeapon(this.GetWeapon1())
	weapon2 := this.GetRoleWeapon().GetWeapon(this.GetWeapon2())

	//主武器
	if weapon1 != nil {
		souls := weapon1.GetWeaponSouls()
		this.AddSoulAttr(souls)
		//套装属性

	}

	//副武器
	if weapon2 != nil {
		souls := weapon2.GetWeaponSouls()
		this.AddSoulAttr(souls)
	}

}

func (this *Role) AddSoulAttr(souls []*bag.Soul) {
	for _, s := range souls {

		//武魂基础属性 加到战斗属性中
		var attack int32 = 0
		var defenses int32 = 0
		var hitPoint int32 = 0
		var critDamage int32 = 0
		var critPower int32 = 0
		var critAllResistance int32 = 0
		//对该武魂攻防血百分比加成
		var soulAttackPower int32 = 0
		var soulDefensesPower int32 = 0
		var soulHitPointPower int32 = 0

		//武魂随机属性
		for id, sattr := range s.SoulAttrs {
			switch id {
			case com_type.SoulAttribute_Attack:
				attack += sattr.Val
			case com_type.SoulAttribute_Defense:
				defenses += sattr.Val
			case com_type.SoulAttribute_HitPoint:
				hitPoint += sattr.Val
			case com_type.SoulAttribute_SoulAttackPower:
				soulAttackPower += sattr.Val
			case com_type.SoulAttribute_SoulDefensePower:
				soulDefensesPower += sattr.Val
			case com_type.SoulAttribute_SoulHitPointPower:
				soulHitPointPower += sattr.Val
			case com_type.SoulAttribute_CritPower:
				critPower += sattr.Val
			case com_type.SoulAttribute_CritDamage:
				critDamage += sattr.Val
			case com_type.SoulAttribute_CritAllResistance:
				critAllResistance += sattr.Val
			}

		}

		//武魂基础属性 + 武魂随机属性加成
		soulDef := table_Soul.Table_.IDMap[s.ConfigId]
		if soulDef != nil {
			attack += soulDef.BaseAttack + soulAttackPower
			defenses += soulDef.BaseDefenses + soulDefensesPower
			hitPoint += soulDef.BaseHitPoint + soulHitPointPower
		}

		//武魂的强化等级属性
		SEDef := table_SoulEnchant.Table_.EnchantLevelMap[s.EnchantLevel]
		if SEDef != nil {
			attack += SEDef.BaseAttack
			defenses += SEDef.BaseDefenses
			hitPoint += SEDef.BaseHitPoint
		}

		roleBattleAttr := this.GetRoleBattleAttr()
		roleBattleAttr.AddBattleAttr(battleAttr.Attack, attack)
		roleBattleAttr.AddBattleAttr(battleAttr.Defenses, defenses)
		roleBattleAttr.AddBattleAttr(battleAttr.HitPoint, hitPoint)
		roleBattleAttr.AddBattleAttr(battleAttr.CritDamage, critDamage)
		roleBattleAttr.AddBattleAttr(battleAttr.CritPower, critPower)
		roleBattleAttr.AddBattleAttr(battleAttr.CritAllResistance, critAllResistance)

	}

}

//加兵器阁的属性 加成为所有兵器最大值 收集为所有武器总和
func (this *Role) AddWeaponAddition() {
	currentLevel, _ := this.GetRoleAttr().GetAttr(attr.Level)
	//功能开启验证
	if int32(currentLevel) < table_GamePlay.Table_.IDMap[com_type.GamePaly_WeaponBook].UnlockLevel {
		return
	}

	roleBattleAttr := this.GetRoleBattleAttr()

	//收集属性
	for _, w := range this.GetRoleWeapon().GetWeaponList() {
		wsID := w.WeaponId*100 + w.WeaponStar
		wsDef := table_WeaponStar.Table_.IDMap[wsID]
		if wsDef != nil {
			roleBattleAttr.AddBattleAttr(wsDef.CollectPowerType, wsDef.CollectPower)
		}
	}

	//加成属性 攻防血 铜钱经验 无双伤害 觉醒伤害
	var maxADH int32 = 0               //攻防血加成
	var maxGE int32 = 0                //铜钱经验
	var maxMusouAttackPower int32 = 0  //无双伤害
	var maxAwakenAttackPower int32 = 0 //觉醒伤害

	for _, w := range this.GetRoleWeapon().GetWeaponList() {
		wsID := w.WeaponId*100 + w.WeaponStar
		wsDef := table_WeaponStar.Table_.IDMap[wsID]
		if wsDef != nil {
			switch w.Class {
			case com_type.WeaponClass_General:
				if wsDef.AdditionPower > maxADH {
					maxADH = wsDef.AdditionPower
				}
			case com_type.WeaponClass_Divine:
				if wsDef.AdditionPower > maxGE {
					maxGE = wsDef.AdditionPower
				}
			case com_type.WeaponClass_AncientGod:
				if wsDef.AdditionPower > maxMusouAttackPower {
					maxMusouAttackPower = wsDef.AdditionPower
				}
			case com_type.WeaponClass_HugeGod:
				if wsDef.AdditionPower > maxAwakenAttackPower {
					maxAwakenAttackPower = wsDef.AdditionPower
				}

			default:
				node_game.Infoln("weapon class is not define")
			}
		}
	}

	roleBattleAttr.AddBattleAttr(battleAttr.AttackPower, maxADH)
	roleBattleAttr.AddBattleAttr(battleAttr.DefensesPower, maxADH)
	roleBattleAttr.AddBattleAttr(battleAttr.HPPower, maxADH)

	roleBattleAttr.AddBattleAttr(battleAttr.GoldPower, maxGE)
	roleBattleAttr.AddBattleAttr(battleAttr.ExpPower, maxGE)

	roleBattleAttr.AddBattleAttr(battleAttr.MusouAttackPower, maxMusouAttackPower)
	roleBattleAttr.AddBattleAttr(battleAttr.AwakenAttackPower, maxAwakenAttackPower)

}

//加载星图（神兵传）属性
func (this *Role) AddStarMap() {
	currentLevel, _ := this.GetRoleAttr().GetAttr(attr.Level)
	//功能开启验证
	if int32(currentLevel) < table_GamePlay.Table_.IDMap[com_type.GamePaly_WeaponBook].UnlockLevel {
		return
	}

	starMap := this.GetRoleStarMap().GetStarMap()
	if starMap == nil {
		return
	}

	SMDef := table_StarMap.Table_.LevelMap[starMap.Level]
	if SMDef != nil {
		roleBattleAttr := this.GetRoleBattleAttr()
		roleBattleAttr.AddBattleAttr(battleAttr.Attack, SMDef.Attack)
		roleBattleAttr.AddBattleAttr(battleAttr.Defenses, SMDef.Defense)
		roleBattleAttr.AddBattleAttr(battleAttr.HitPoint, SMDef.HitPoint)
	}

}

//根据百分比计算总的属性
func (this *Role) AddAddition() {
	_BattleAttrRate := float32(GlobalConstData.BattleAttrRate)

	roleBattleAttr := this.GetRoleBattleAttr()
	Attack, _ := this.battleAttr.GetBattleAttr(battleAttr.Attack)
	AttackPower, _ := this.battleAttr.GetBattleAttr(battleAttr.AttackPower)
	Attack = int32(float32(Attack) * float32(AttackPower) / _BattleAttrRate)
	roleBattleAttr.AddBattleAttr(battleAttr.Attack, Attack)

	Defenses, _ := this.battleAttr.GetBattleAttr(battleAttr.Defenses)
	DefensesPower, _ := this.battleAttr.GetBattleAttr(battleAttr.DefensesPower)
	Defenses = int32(float32(Defenses) * float32(DefensesPower) / _BattleAttrRate)
	roleBattleAttr.AddBattleAttr(battleAttr.Defenses, Defenses)

	HitPoint, _ := this.battleAttr.GetBattleAttr(battleAttr.HitPoint)
	HPPower, _ := this.battleAttr.GetBattleAttr(battleAttr.HPPower)
	HitPoint = int32(float32(HitPoint) * float32(HPPower) / _BattleAttrRate)
	roleBattleAttr.AddBattleAttr(battleAttr.Defenses, HitPoint)

	ElementalDefenses, _ := this.battleAttr.GetBattleAttr(battleAttr.ElementalDefenses)
	ElementalDefensesPower, _ := this.battleAttr.GetBattleAttr(battleAttr.ElementalDefensesPower)
	ElementalDefenses = int32(float32(ElementalDefenses) * float32(ElementalDefensesPower) / _BattleAttrRate)
	roleBattleAttr.AddBattleAttr(battleAttr.Defenses, ElementalDefenses)

}

//战斗属性变更处理
func (this *Role) OnBattleAttrDirty() {
	if this.GetSelectRoleOk() {
		node_game.Infoln(this.GetID(), "OnBattleAttrDirty")
		msg := &cs_message.RoleBattleAttrToC{
			BattleAttrs: this.battleAttr.PackAllBattleAttr(),
		}

		this.user.Post(msg)
	}
}
